package com.hengpeng.itfints.service.notice.impl;

import java.util.List;

import org.hibernate.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfinbase.persist.ProjectInfo;
import com.hengpeng.itfinbase.persist.ProjectInfo.FundType;
import com.hengpeng.itfinbase.persist.ProjectInfo.InvestType;
import com.hengpeng.itfinbase.persist.ProjectInfo.ProjectType;
import com.hengpeng.itfinbase.persist.ProjectInfoContacts;
import com.hengpeng.itfints.service.ServiceFactory;
import com.hengpeng.itfints.service.notice.ProjectService;

@Lazy
@Service
public class ProjectServiceImpl implements ProjectService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<ProjectInfo>> queryList(Integer pageIndex, Integer pageSize,String bussinessType,
			String location, String financeType,String gm,ProjectType projectType, Long publishUserId
			,InvestType investType,FundType fundType,String tzje)
	{
		DynamicQuery query = new DynamicQuery(ProjectInfo.class);
		if (investType != null) {
			query.eq("investType", investType);
		}
		if (fundType != null) {
			query.eq("fundType", fundType);
		}
		if (!StringHelper.isEmpty(tzje)) {
			if ("less200".equals(gm)) {
				query.lt("minInvestMoney", 2000000);
			} else if ("200to500".equals(gm)) {
				query.ge("minInvestMoney", 2000000);
				query.lt("maxInvestMoney", 5000000);
			} else if ("500to800".equals(gm)) {
				query.ge("minInvestMoney", 5000000);
				query.lt("maxInvestMoney", 8000000);
			} else if ("morethan800".equals(gm)) {
				query.ge("maxInvestMoney", 8000000);
			}
		}
		
		if (!StringHelper.isEmpty(bussinessType)) {
			query.eq("bussinessType", ProjectInfo.BussinessType.valueOf(bussinessType));
		}
		if (!StringHelper.isEmpty(location)) {
			query.eq("location", ProjectInfo.Location.valueOf(location));
		}
		if (!StringHelper.isEmpty(financeType)) {
			query.eq("financeType", ProjectInfo.FinanceType.valueOf(financeType));
		}
		if (publishUserId != null) {
			query.eq("publishUserId", publishUserId);
		}
		if (!StringHelper.isEmpty(gm)) {
			if ("less200".equals(gm)) {
				query.lt("financeMoney", 2000000);
			} else if ("200to500".equals(gm)) {
				query.ge("financeMoney", 2000000);
				query.lt("financeMoney", 5000000);
			} else if ("500to800".equals(gm)) {
				query.ge("financeMoney", 5000000);
				query.lt("financeMoney", 8000000);
			} else if ("morethan800".equals(gm)) {
				query.ge("financeMoney", 8000000);
			}
		}
		if (projectType != null) {
			query.eq("projectType", projectType);
		}
		query.desc("createTime"); 
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);
		QueryResult<ProjectInfo> requery = serviceFactory.getPersistService().findWithPage(ProjectInfo.class, query);
		Long count = requery.getTotalCount();
		ServiceResult<List<ProjectInfo>> serviceResult = new ServiceResult<List<ProjectInfo>>();// 设置返回值
		serviceResult.setReturnValue(requery.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}

	@Override
	public ServiceResult<List<ProjectInfoContacts>> myreceiveList(Integer pageIndex, Integer pageSize, Long receiveUserId) {
		DynamicQuery query = new DynamicQuery(ProjectInfoContacts.class);
		if (receiveUserId != null) {
			query.eq("receiveUserId", receiveUserId);
		}
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);
		QueryResult<ProjectInfoContacts> requery = serviceFactory.getPersistService().findWithPage(ProjectInfoContacts.class, query);
		Long count = requery.getTotalCount();
		ServiceResult<List<ProjectInfoContacts>> serviceResult = new ServiceResult<List<ProjectInfoContacts>>();// 设置返回值
		serviceResult.setReturnValue(requery.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}

	@Override
	public ServiceResult<List<ProjectInfoContacts>> myorderList(Integer pageIndex, Integer pageSize, Long orderUserId) {
		DynamicQuery query = new DynamicQuery(ProjectInfoContacts.class);
		if (orderUserId != null) {
			query.eq("startUserId", orderUserId);
		}
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);
		QueryResult<ProjectInfoContacts> requery = serviceFactory.getPersistService().findWithPage(ProjectInfoContacts.class, query);
		Long count = requery.getTotalCount();
		ServiceResult<List<ProjectInfoContacts>> serviceResult = new ServiceResult<List<ProjectInfoContacts>>();// 设置返回值
		serviceResult.setReturnValue(requery.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}
}
